/*******************************************************************************

PooledRegions MASTER.do

2022.09.22 (DJ) Created

This is the master do file that replicates analysis in a paper titled 
"Market Access, Trade Costs, and Technology Adoption: Evidence from Northern Tanzania"

********************************************************************************/
* General Settings
********************************************************************************

set more off, perm
clear all
set maxvar 20000
graph set window fontface "Times New Roman"


* Standardize settings accross users

ieboilstart, version(14.0)   // Confirm Stata version that runs the code   
`r(version)'                    

* Install necessary packages

local packages estout // Add required user-written commands
    foreach command of local packages {
        cap which `command'
        if _rc == 111 ssc install `command'
    }

// Also download cluster2.ado file for running regressions in dyad form from this link:
// https://www.kellogg.northwestern.edu/faculty/petersen/htm/papers/se/se_programming.htm 



* Set globals and directory
cap noisily cd "YOUR_OWN_PATH"

global analysis "Data"
global pool_code "Code"
global pool_results "Results"
global google "Data/google"
global maizeprice ""
global type urea
global elasticity -7.39




/***********************************
	manuscript
***********************************/

** Table 1
do "${pool_code}/PooledRegions analysis_sumstats.do"

** Table 2
do "${pool_code}/PooledRegions analysis_FARMER_robust.do"

** Table 3 Panel A, B
do "${pool_code}/PooledRegions analysis_villagelevelaccess.do"

** Table 3 Panel C
** obtained from the same code for Table 2 above

** Table 4 and Table B5

	** Uses R Code
	** Step 1.  Run Trade Costs Replication.R

	** Step 2.

	do "${pool_code}/VillageMNL_Replication.do"

** Table 5

	** Step 1. 
	
	do "${pool_code}/Individual Modeling Replication.do"
	
	** Step 2.  Run Counterfactuals Replication Basline.R

** Figure 1
do "${pool_code}/PooledRegions analysis_CDFs.do"


/***********************************
	appendix A
***********************************/

** Table A2, A3
do "${pool_code}/PooledRegions analysis_travelcost.do"

** Table A4
do "${pool_code}/PooledRegions analysis_inputoutput_locations.do"

** Table A5
do "${pool_code}/PooledRegions analysis_TableA5.do"

** Table A6
do "${pool_code}/PooledRegions analysis_agrovet.do"

** Table A7 Panel A
** obtained from the same code for Table 3 above 

** Table A7 Panel B
** obtained from the same code for Table 2 above

** Table A8 Panel A
** obtained from the same code for Table 3 above 

** Table A8 Panel B
do "${pool_code}/PooledRegions analysis_villagelevelaccess_robust.do"

** Table A8 Panel C and D
do "${pool_code}/robustness_travelcost/PooledRegions travelcost_robust_Master.do"

** Figure A2
** obtained from the same code for Figure 1 above 

** Figure A3
do "${pool_code}/PooledRegions analysis_FigA3.do"

** Figure A4
do "${pool_code}/PooledRegions analysis_dist_AV.do"



/***********************************
	appendix B
***********************************/

** Table B1

	do "${pool_code}/Individual Modeling Replication.do"
	
** Table B2

	do "${pool_code}/IV Compile Replication.do"
	
** Table B3

	do "${pool_code}/ProductionFunction Replication.do"
	
** Table B4-1 and B4-2

	** Run in R only after earlier tables
	
	** Counterfactuals Replication Sensitivity.R
	
** Table B5

	** See Table 4

** Table B6

	** Run in R only after earlier tables
	
	** Counterfactuals Replication Price Shock.R

** Table B7
	
	** Step 1:  Run Individual Modeling Trimming Values Replication.do
	
	** Step 2:  Counterfactuals Replication Trimming.R

/***********************************
	appendix C
***********************************/
** Table C2 and C3
do "${pool_code}/ex_valid_dyad_price.do"

do "${pool_code}/ex_valid_price_analysis.do"

** Table C4
do "${pool_code}/ex_valid_lsms_fert_adoption.do"


/***********************************
	appendix D
***********************************/

** Figure D1
** obtained from the same code for Figure 1 above 

** Table D1
** obtained from the same code for Table 3 above 

** Table D2
** obtained from the same code for Table 2 above

